package android.storefront.common.utils.activity;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.storefront.activity.R;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class MainMenu {
	
	private int[] resArray = new int[] {
			R.drawable.add48, R.drawable.communicate48,
			R.drawable.wrench48, R.drawable.save48
	};
	
	private String[] title = new String[]{
			"添加歌曲", "歌曲信息", "查找歌词", "更多.."
	};
	
	
	private int[] subResArray = new int[] {
			R.drawable.prohibit48, R.drawable.key48,
			R.drawable.folder_info48, R.drawable.folder_alert48,
			R.drawable.faq48, R.drawable.edit48,
			R.drawable.delete_user48, R.drawable.communicate48
	};
	private String[] subTitle = new String[]{
			"添加歌曲", "歌曲信息", "查找歌词", "更多..",
			"添加歌曲", "歌曲信息", "查找歌词", "更多.."
	};
	
	
	private PopupWindow mainPopupWindow = null;
	private PopupWindow subPopupWindow = null;
	
	private Context context = null;
	private View tokenView = null;
	
	private MainMenu(Context context,View tokenView) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.tokenView = tokenView;
		initPopupWindow();
	}
	
	private static MainMenu mainMenu = null;
	
	public static MainMenu getInstance(Context context,View tokenView){
		if(null == mainMenu){
			return new MainMenu(context, tokenView);
		}else{
			return mainMenu;
		}
	}
	
	public void responseOnKeyDown(int keyCode, KeyEvent event){
		if(keyCode == KeyEvent.KEYCODE_MENU && mainPopupWindow.isShowing()){
			mainPopupWindow.dismiss();
			if(subPopupWindow.isShowing()){
				subPopupWindow.dismiss();
			}
		}else if(keyCode == KeyEvent.KEYCODE_MENU && !mainPopupWindow.isShowing()){
			mainPopupWindow.showAtLocation(tokenView, Gravity.BOTTOM, 0, 35);
		}else{
			
		}
	}
	
	public void responseOnKeyDown(){
		if(mainPopupWindow.isShowing()){
			mainPopupWindow.dismiss();
			if(subPopupWindow.isShowing()){
				subPopupWindow.dismiss();
			}
		}else if(!mainPopupWindow.isShowing()){
			mainPopupWindow.showAtLocation(tokenView, Gravity.BOTTOM, 0, 35);
		}else{
			
		}
	}
	
	
	private void initPopupWindow(){
		
		View view = LayoutInflater.from(context).inflate(R.layout.main_menu, null);
		GridView mainGridView = (GridView)view.findViewById(R.id.menuGridChange);
		mainGridView.setAdapter(new MainImageAdapter(context));
		//用Popupwindow弹出menu
		mainPopupWindow = new PopupWindow(view,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		mainPopupWindow.setAnimationStyle(R.style.PopupAnimation);
		
		
		View subView = LayoutInflater.from(context).inflate(R.layout.sub_menu, null);
        GridView subGridView = (GridView)subView.findViewById(R.id.menuGridChange);
        subGridView.setAdapter(new SubImageAdapter(context));
        subPopupWindow = new PopupWindow(subView,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        subPopupWindow.setAnimationStyle(R.style.PopupAnimation);
	}
	
	
	public class MainImageAdapter extends BaseAdapter {
			
			private Context context;
			
			public MainImageAdapter(Context context) {
				this.context = context;
			}
			@Override
			public int getCount() {
				return resArray.length;
			}
			@Override
			public Object getItem(int arg0) {
				return resArray[arg0];
			}
			@Override
			public long getItemId(int arg0) {
				return arg0;
			}
			@Override
			public View getView(int arg0, View arg1, ViewGroup arg2) {
				LinearLayout linear = new LinearLayout(context);
				linear.setBackgroundResource(R.drawable.menu_click_drawable);
				linear.setOrientation(LinearLayout.VERTICAL);
				
				ImageView iv = new ImageView(context);
				iv.setImageBitmap(((BitmapDrawable)context.getResources().getDrawable(resArray[arg0])).getBitmap());
				LinearLayout.LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				params2.gravity=Gravity.CENTER;
				linear.addView(iv, params2);
				
				TextView tv = new TextView(context);
				tv.setText(title[arg0]);
				LinearLayout.LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				params3.gravity=Gravity.CENTER;
				
				linear.addView(tv, params3);
				
				
				//实现ontouch事件
				if(3 == arg0){
					linear.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							if(subPopupWindow.isShowing()){
								subPopupWindow.dismiss();
							}else if(!subPopupWindow.isShowing()){
								subPopupWindow.showAtLocation(tokenView, Gravity.BOTTOM, 0, 125);
							}else{
								
							}
						}
					});
				}else{
					linear.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
						}
					});
				}
				
				
				return linear;
			}
		}
	
	
		public class SubImageAdapter extends BaseAdapter {
				
				private Context context;
				
				public SubImageAdapter(Context context) {
					this.context = context;
				}
				
				@Override
				public int getCount() {
					return subResArray.length;
				}
		
				@Override
				public Object getItem(int arg0) {
					return subResArray[arg0];
				}
		
				@Override
				public long getItemId(int arg0) {
					return arg0;
				}
		
				@Override
				public View getView(int arg0, View arg1, ViewGroup arg2) {
					LinearLayout linear = new LinearLayout(context);
					linear.setOrientation(LinearLayout.VERTICAL);
					
					ImageView iv = new ImageView(context);
					iv.setImageBitmap(((BitmapDrawable)context.getResources().getDrawable(subResArray[arg0])).getBitmap());
					LinearLayout.LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
					params2.gravity=Gravity.CENTER;
					linear.addView(iv, params2);
					
					TextView tv = new TextView(context);
					tv.setText(subTitle[arg0]);
					LinearLayout.LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
					params3.gravity=Gravity.CENTER;
					
					linear.addView(tv, params3);
					
					//实现ontouch事件
					
					return linear;
				}
			}
}
